package com.example.friend.controller;

import com.example.core.constants.HttpConstants;
import com.example.core.controller.BaseController;
import com.example.core.result.Res;
import com.example.friend.entity.DTO.LoginUserDTO;
import com.example.friend.entity.DTO.UserUpdateDTO;
import com.example.friend.entity.VO.LoginUserVO;
import com.example.friend.entity.VO.UserDetailVO;
import com.example.friend.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    @GetMapping("sendCode")
    public Res<Void> sendCode(@RequestParam(value = "phone")String phone) {
        return super.toR(userService.sendCode(phone)) ;
    }

    @PostMapping("/login")
    public Res<String> login(@RequestBody LoginUserDTO loginUserDTO) {
        return Res.ok(userService.login(loginUserDTO.getPhone(), loginUserDTO.getCode()));
    }

    @DeleteMapping("/logout")
    public Res<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return super.toR(userService.logout(token));
    }

    @GetMapping("/info")
    public Res<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        LoginUserVO loginUserVO = userService.getLoginUserInfo(token);
        return loginUserVO == null ? Res.fail() : Res.ok(loginUserVO);
    }

    @GetMapping("/detail")
    public Res<UserDetailVO> detail() {
        return Res.ok(userService.detail());
    }

    @PutMapping("/edit")
    public Res<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toR(userService.edit(userUpdateDTO));
    }

    @PutMapping("/head-image/update")
    public Res<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toR(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }

}
